package com.live.dengdengtv.fragment;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.live.dengdengtv.R;
import com.live.dengdengtv.adapter.MsgAdapter;
import com.live.dengdengtv.bean.AppConstants;
import com.live.dengdengtv.bean.AppInfo;
import com.live.dengdengtv.bean.SimpleInfoData;
import com.live.dengdengtv.bean.gson.ConversationCard;
import com.live.dengdengtv.bean.gson.ConversationSp;
import com.live.dengdengtv.bean.gson.PersonInfoItem;
import com.live.dengdengtv.fragment.hall.BlankFragment;
import com.live.dengdengtv.net.NetTaskList;
import com.live.dengdengtv.view.SlidingTabLayout;
import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMMessage;
import com.tencent.TIMValueCallBack;

import java.util.ArrayList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;

/**
 */
public class MsgFragment extends BaseFragment {

    private static final String TAG = MsgFragment.class.getName();
    private ViewPager mViewPager;
    private FragmentPagerAdapter pagerAdapter;

    private SlidingTabLayout mSlidingTabLayout;

    private ArrayList<PersonInfoItem> friendList = new ArrayList<PersonInfoItem>();


    private List<ConversationCard> conversationFriend = new ArrayList<>();
    private List<ConversationCard> conversationStranger = new ArrayList<>();
    private ConversationSp conversationSp;

//    private StrangerMsgFrag strangerMsgFrag = new StrangerMsgFrag();
    private ConversationFrag friendMsgFrag = new ConversationFrag();
    private ConversationFrag strangerMsgFrag = new ConversationFrag();
    private NoticeFrag noticeMsgFrag = new NoticeFrag();

    public void setOnItemClickListener(ConversationItemClick onItemClickListener){
        friendMsgFrag.setOnItemClickListener(onItemClickListener);
        strangerMsgFrag.setOnItemClickListener(onItemClickListener);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        friendMsgFrag.setListData(conversationFriend);
        strangerMsgFrag.setListData(conversationStranger);

        getFriendList();

        View view = inflater.inflate(R.layout.fragment_msg, container, false);

        mViewPager = (ViewPager) view.findViewById(R.id.viewpager);
        pagerAdapter = new MsgFragmentPagerAdapter(getChildFragmentManager());
        mViewPager.setAdapter(pagerAdapter);

        initSlidingTabLayout(view);

        Bundle bundle = getArguments();
        if(bundle!= null&& !TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_ALERT))){
            mViewPager.setCurrentItem(2);
        }

        return view;
    }

    private void getFriendList(){
        requestNetData(NetTaskList.friend_list, AppInfo.getHeadInfo(),1,false);
    }

    @Override
    public boolean onResponse(SimpleInfoData response, int id) {
        if (id == 1) {
            friendList.clear();
            if(response.result == 0){
                JsonArray array = response.data.getAsJsonArray();
                if(array == null || array.size()==0) return false;
                for (int i=0; i<array.size() ;i++){
                    PersonInfoItem liveItem = new Gson().fromJson(array.get(i).getAsJsonObject(), PersonInfoItem.class);
                    friendList.add(liveItem);
                }
            }
            getConversationCard();
        }
        return false;
    }


    private void getConversationCard(){
        SharedPreferences sp = getActivity().getSharedPreferences(AppConstants.SP_CONVERSATIOM, Context.MODE_PRIVATE);
        String conJson = sp.getString(AppConstants.KEY_CONVERSATIOM,"");

        if(!TextUtils.isEmpty(conJson)){
            Log.e(TAG,"conJson="+conJson);
            conversationSp = new Gson().fromJson(conJson, ConversationSp.class);
        }else {
            conversationSp = new ConversationSp();
        }

        if(conversationSp.conCardList == null || conversationSp.conCardList.size() == 0){
            Log.e(TAG,"conCardList空");
            return;
        }

        if(friendList.size() == 0){
            Log.e(TAG,"friendList.size() == 0");
            conversationStranger.addAll(conversationSp.conCardList);
        }else {

            for(ConversationCard item: conversationSp.conCardList){
                for(PersonInfoItem person: friendList){
                    if(item.user_id.equals(person.getConcern_id())){
                        conversationFriend.add(item);
                    }else {
                        conversationStranger.add(item);
                    }
                }
            }
        }

//        pagerAdapter.notifyDataSetChanged();
        friendMsgFrag.notifyDataSetChanged();
        strangerMsgFrag.notifyDataSetChanged();
        Log.e(TAG,"conversationFriend=" +conversationFriend.size());
        Log.e(TAG,"conversationStranger=" +conversationStranger.size());

    }

    private void initSlidingTabLayout(View view) {
        mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
        mSlidingTabLayout.setCustomTabView(R.layout.hall_tabtv, R.id.tab_tv);

        final float density = getResources().getDisplayMetrics().density;
        mSlidingTabLayout.setNavigationThick((int) (density*5));
        mSlidingTabLayout.setDefaultTextColor(0xFFECE8E9);
        mSlidingTabLayout.setSelectedTextColor(0xFFED242C);

        mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
            @Override
            public int getIndicatorColor(int position) {
                return 0xFFED242C;
            }
            @Override
            public int getDividerColor(int position) {
                return 0x00FFFFFF;
            }
        });

        mSlidingTabLayout.setViewPager(mViewPager);
    }

    private FragmentStopLis fragmentStopLis;
    public void setOnStopLis(FragmentStopLis fragmentStopLis){
        this.fragmentStopLis = fragmentStopLis;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(fragmentStopLis!=null){
            fragmentStopLis.onFragmentStop();
            fragmentStopLis = null;
        }
    }



    private class MsgFragmentPagerAdapter extends FragmentPagerAdapter {
        private String[] titles = new String[]{"好友","未关注的人","通知"};

        MsgFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment;
            if (i == 0) {
                fragment = friendMsgFrag;
            } else if (i == 1) {
                fragment = strangerMsgFrag;
            } else {
                fragment = noticeMsgFrag;

            }
            return fragment;
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }


}
